/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package loginview;

import static java.lang.Thread.sleep;
import java.sql.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import net.proteanit.sql.DbUtils;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;


public class OutcomeViewFrame extends javax.swing.JFrame {

    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;
    
    /**
     * Creates new form Outcome
     */
    public OutcomeViewFrame() {
        initComponents();
        conn = JavaConnect.ConnecrDb();
        Update_OutcomeTable();
        CurrentDate();
        OutcomePrice();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        homeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lbl_date = new javax.swing.JLabel();
        lbl_time = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        outcomeTable = new javax.swing.JTable();
        deleteButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lbl_outcome = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setLocation(new java.awt.Point(120, 20));
        setPreferredSize(new java.awt.Dimension(1050, 765));

        jToolBar1.setRollover(true);

        homeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/loginview/green-home-icon.png"))); // NOI18N
        homeButton.setText("Home");
        homeButton.setFocusable(false);
        homeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        homeButton.setMaximumSize(new java.awt.Dimension(75, 60));
        homeButton.setPreferredSize(new java.awt.Dimension(75, 60));
        homeButton.setSize(new java.awt.Dimension(75, 60));
        homeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        homeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(homeButton);

        jLabel1.setMaximumSize(new java.awt.Dimension(650, 55));
        jLabel1.setPreferredSize(new java.awt.Dimension(650, 55));
        jToolBar1.add(jLabel1);

        lbl_date.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        lbl_date.setForeground(new java.awt.Color(51, 51, 51));
        lbl_date.setText("Date");
        lbl_date.setMaximumSize(new java.awt.Dimension(150, 55));
        lbl_date.setPreferredSize(new java.awt.Dimension(150, 55));
        jToolBar1.add(lbl_date);

        lbl_time.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        lbl_time.setForeground(new java.awt.Color(51, 51, 51));
        lbl_time.setText("Time");
        lbl_time.setMaximumSize(new java.awt.Dimension(150, 55));
        lbl_time.setPreferredSize(new java.awt.Dimension(150, 55));
        jToolBar1.add(lbl_time);

        outcomeTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(outcomeTable);

        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/loginview/trash-icon.png"))); // NOI18N
        deleteButton.setText("Delete");
        deleteButton.setMaximumSize(new java.awt.Dimension(97, 30));
        deleteButton.setPreferredSize(new java.awt.Dimension(97, 30));
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Outcome", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 1, 14), new java.awt.Color(51, 51, 51))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(190, 50));

        lbl_outcome.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        lbl_outcome.setForeground(new java.awt.Color(51, 51, 51));
        lbl_outcome.setMaximumSize(new java.awt.Dimension(0, 0));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbl_outcome, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lbl_outcome, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1038, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(369, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Update_OutcomeTable(){
    String sql = "SELECT * FROM Outcome";
    try{
    pst = conn.prepareStatement(sql);
    rs = pst.executeQuery();
    outcomeTable.setModel(DbUtils.resultSetToTableModel(rs));
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    finally {
    try{
    rs.close(); 
    pst.close(); }
    catch(Exception e) { } }
}
    
    private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonActionPerformed
        MainViewFrame mainFrame = new MainViewFrame();
        mainFrame.setVisible(true);
        dispose();
        
    }//GEN-LAST:event_homeButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int p = JOptionPane.showConfirmDialog(null, "Do you really want to delete?","Delete", JOptionPane.YES_NO_OPTION);
        int row = outcomeTable.getSelectedRow();
        String tableClick = (outcomeTable.getModel().getValueAt(row, 0).toString());    
        if(p==0){ 
        try{
         String sql = "Delete  FROM Outcome where ID = '"+tableClick+"' ";  
         pst = conn.prepareStatement(sql);
             pst.execute();
         if(rs.next()){
             
         }
        }     
            
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
        
        finally {
        try{
        rs.close(); 
        pst.close(); }
        catch(Exception e) { } }
        
        Update_OutcomeTable();
        }


    }//GEN-LAST:event_deleteButtonActionPerformed

    public void CurrentDate(){
       Thread clock =new Thread(){
       public void run(){
           for(;;){
        Calendar cal = new GregorianCalendar();
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int year = cal.get(Calendar.YEAR);
        lbl_date.setText("Date "+day+"/"+(month+1)+"/"+year);
        
       int second = cal.get(Calendar.SECOND);
       int minute = cal.get(Calendar.MINUTE);
       int hour = cal.get(Calendar.HOUR_OF_DAY);
       lbl_time.setText("Time "+hour+":"+minute+":"+second);
               try {
                   sleep(1000);
               } catch (InterruptedException ex) {
                   Logger.getLogger(MainViewFrame.class.getName()).log(Level.SEVERE, null, ex);
               }
           }
       }
       
       };
        clock.start();
    }
    
    
    public void OutcomePrice(){
        try{
           String sql = "SELECT sum(Maintenance), sum(Salary) FROM Outcome";
           
           
           pst = conn.prepareStatement(sql);
           rs = pst.executeQuery();
            Double add1 = rs.getDouble("sum(Maintenance)");
            Double add2 = rs.getDouble("sum(Salary)");
            Double result = (add1+add2);
            lbl_outcome.setText(result.toString());
            
        } catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(OutcomeViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(OutcomeViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(OutcomeViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(OutcomeViewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new OutcomeViewFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton homeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lbl_date;
    private javax.swing.JLabel lbl_outcome;
    private javax.swing.JLabel lbl_time;
    private javax.swing.JTable outcomeTable;
    // End of variables declaration//GEN-END:variables
}
